// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/connectors/hive/hive_partition_util.h>
#include <pollux/testing/gtest_utils.h>
#include <pollux/dwio/catalog/hive/file_utils.h>
#include <pollux/testing/vector/vector_test_base.h>

#include <gtest/gtest.h>

using namespace kumo;
using namespace kumo::pollux;
using namespace kumo::pollux::connector::hive;
using namespace kumo::pollux::dwio::catalog::hive;

class HivePartitionUtilTest : public ::testing::Test,
                              public pollux::VectorBuilder {
 protected:
  template <typename T>
  VectorPtr makeDictionary(const std::vector<T>& data) {
    auto base = make_flat_vector(data);
    auto indices =
        make_indices(data.size() * 10, [](auto row) { return row / 10; });
    return wrap_in_dictionary(indices, data.size(), base);
  };

  RowVectorPtr makePartitionsVector(
      RowVectorPtr input,
      const std::vector<column_index_t>& partitionChannels) {
    std::vector<VectorPtr> partitions;
    std::vector<std::string> partitonKeyNames;
    std::vector<TypePtr> partitionKeyTypes;

    RowTypePtr inputType = as_row_type(input->type());
    for (column_index_t channel : partitionChannels) {
      partitions.push_back(input->childAt(channel));
      partitonKeyNames.push_back(inputType->nameOf(channel));
      partitionKeyTypes.push_back(inputType->childAt(channel));
    }

    return std::make_shared<RowVector>(
        pool(),
        ROW(std::move(partitonKeyNames), std::move(partitionKeyTypes)),
        nullptr,
        input->size(),
        partitions);
  }
};

TEST_F(HivePartitionUtilTest, partitionName) {
  {
    RowVectorPtr input = make_row_vector(
        {"flat_bool_col",
         "flat_tinyint_col",
         "flat_smallint_col",
         "flat_int_col",
         "flat_bigint_col",
         "dict_string_col",
         "const_date_col"},
        {make_flat_vector<bool>(std::vector<bool>{false}),
         make_flat_vector<int8_t>(std::vector<int8_t>{10}),
         make_flat_vector<int16_t>(std::vector<int16_t>{100}),
         make_flat_vector<int32_t>(std::vector<int32_t>{1000}),
         make_flat_vector<int64_t>(std::vector<int64_t>{10000}),
         makeDictionary<StringView>(std::vector<StringView>{"str1000"}),
         make_constant<int32_t>(10000, 1, DATE())});

    std::vector<std::string> expectedPartitionKeyValues{
        "flat_bool_col=false",
        "flat_tinyint_col=10",
        "flat_smallint_col=100",
        "flat_int_col=1000",
        "flat_bigint_col=10000",
        "dict_string_col=str1000",
        "const_date_col=1997-05-19"};

    std::vector<column_index_t> partitionChannels;
    for (auto i = 1; i <= expectedPartitionKeyValues.size(); i++) {
      partitionChannels.resize(i);
      std::iota(partitionChannels.begin(), partitionChannels.end(), 0);

      EXPECT_EQ(
          FileUtils::makePartName(
              extractPartitionKeyValues(
                  makePartitionsVector(input, partitionChannels), 0),
              true),
          melon::join(
              "/",
              std::vector<std::string>(
                  expectedPartitionKeyValues.data(),
                  expectedPartitionKeyValues.data() + i)));
    }
  }

  // Test unsupported partition type.
  {
    RowVectorPtr input = make_row_vector(
        {"map_col"},
        {make_map_vector<int32_t, StringView>(
            {{{1, "str1000"}, {2, "str2000"}}})});

    std::vector<column_index_t> partitionChannels{0};

    POLLUX_ASSERT_THROW(
        FileUtils::makePartName(
            extractPartitionKeyValues(
                makePartitionsVector(input, partitionChannels), 0),
            true),
        "Unsupported partition type: MAP");
  }
}

TEST_F(HivePartitionUtilTest, partitionNameForNull) {
  std::vector<std::string> partitionColumnNames{
      "flat_bool_col",
      "flat_tinyint_col",
      "flat_smallint_col",
      "flat_int_col",
      "flat_bigint_col",
      "flat_string_col",
      "const_date_col"};

  RowVectorPtr input = make_row_vector(
      partitionColumnNames,
      {make_nullable_flat_vector<bool>({std::nullopt}),
       make_nullable_flat_vector<int8_t>({std::nullopt}),
       make_nullable_flat_vector<int16_t>({std::nullopt}),
       make_nullable_flat_vector<int32_t>({std::nullopt}),
       make_nullable_flat_vector<int64_t>({std::nullopt}),
       make_nullable_flat_vector<StringView>({std::nullopt}),
       make_constant<int32_t>(std::nullopt, 1, DATE())});

  for (auto i = 0; i < partitionColumnNames.size(); i++) {
    std::vector<column_index_t> partitionChannels = {(column_index_t)i};
    auto partitionEntries = extractPartitionKeyValues(
        makePartitionsVector(input, partitionChannels), 0);
    EXPECT_EQ(1, partitionEntries.size());
    EXPECT_EQ(partitionColumnNames[i], partitionEntries[0].first);
    EXPECT_EQ("", partitionEntries[0].second);
  }
}
